﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GA.Server.Api;

namespace GA.Server.Main.Actions
{
    public class ServerInfoAction : IConsoleAction
    {
        #region IConsoleAction Members

        public string Name
        {
            get { return "ServerInfo"; }
        }

        public string HelpInfo
        {
            get
            {
                StringBuilder help = new StringBuilder();
                help.AppendLine("ServerInfo Command:");
                help.AppendLine(" USAGE: ServerInfo [<ServerName/ProcessId>]");
                help.AppendLine(" DESCRIPTION: Gives information on servers currently running.");
                return help.ToString();
            }
        }

        public ConsoleActionResult RunAction(string param)
        {
            int processId = 0;
            List<GAServer> servers = new List<GAServer>();
            if (param != null && param.Length > 0)
            {
                if (int.TryParse(param, out processId))
                {
                    if (Program.CurrentProgram.RunningServers.Any((server) => { return server.ProcessId == processId; }))
                    {
                        servers.Add(Program.CurrentProgram.RunningServers.First((server) => { return server.ProcessId == processId; }));
                    }
                }
                else
                {
                    if (Program.CurrentProgram.RunningServers.Any((server) => { return server.Name.Equals(param, StringComparison.InvariantCultureIgnoreCase); }))
                    {
                        servers.AddRange(Program.CurrentProgram.RunningServers.TakeWhile((server) => { return server.Name.Equals(param, StringComparison.InvariantCultureIgnoreCase); }));
                    }
                }
            }
            else
            {
                servers.AddRange(Program.CurrentProgram.RunningServers);
            }
            Console.Out.WriteLine("SERVER               PROCESSID   STATUS          UPTIME");
            foreach (GAServer server in servers)
            {
                Console.Out.WriteLine("{0} {1} {2} {3}",
                    TrimOrAddCharacters(server.Name, 20),
                    TrimOrAddCharacters(server.ProcessId.ToString(), 11),
                    TrimOrAddCharacters(server.Status.ToString(), 15),
                    server.StartedDate == null ? "--" : DateTime.Now.Subtract(server.StartedDate).ToString());
            }
            if (servers.Count == 0)
            {
                Console.Out.WriteLine("No servers found....");
            }
            return ConsoleActionResult.CompletedSuccessfully;
        }

        private string TrimOrAddCharacters(string originalString, int desiredLength)
        {
            if (originalString.Length > desiredLength)
            {
                return originalString.Remove(desiredLength);
            }
            else if (originalString.Length < desiredLength)
            {
                return originalString.PadRight(desiredLength);
            }
            return originalString;
        }

        #endregion
    }
}
